Array< Employee > workerList( 100 );in a C++ program.
Array< Employee > workerList;in a C++ program?
template< class T > Array< T >::Array( int s )
template< class T1 > class C1Describe the friendship relationships established by placing each of the following friendship declarations inside this class template header. Identifiers beginning with "f" are functions, identifiers beginning with "C" are classes, and identifiers beginning with "T" can represent any type (i.e., built-in types or class types).
a) friend void f1();b) friend void f2( C1< T1 > &);
c) friend void C2::f4();
d) friend void C3< T1 >::f5( C1< T1 > & );
e) friend class C5;
f) friend class C6< T1 >;
template< class T >or
template< class ElementType >or
template< class BorderType, class FillType >The formal parameters of a template definition are used (as they would be with arguments of built-in types or user-defined types) to specify the types of the arguments to the function, to specify the return type of the function, and to declare variables within the function.
void printArray( const int *array, const int count )Every formal parameter in a function template definition should normally appear in the function's{
for ( int i = 0; i < count; i++ )
cout << array[ i ] << " ";
cout << endl;
}
printArray( a, aCount );
printArray( b, bCount );causes the compiler to instantiate a second printArray template function for which type parameter T is double. The call
printArray( c, cCount );causes the compiler to instantiate a third printArray template function for which type parameter T is char.
void printArray( const int *, const int );void printArray( const double *, const int );
void printArray( const char *, const int );
template< class T >to specify that this is a class template definition with type parameter T indicating the type of the Stack class to be created. The programmer need not specifically use identifier T--any identifier can be used. The type of element to be stored on this Stack is mentioned only generically as T throughout the Stack class header and member function definitions. We will show
Stack< int > intStack;
template< class T >Then each definition resembles a conventional function definition except that the Stack element type is always listed generically as type parameter T. The binary scope
stackPtr = new T[ size ];in the Stack class template definition is generated by the compiler in the Stack< double > template class as
stackPtr = new double[ size ];
testStack( doubleStack, 1.1, 1.1, "doubleStack" );Note that the output oftestStack( intStack, 1, 1, "intStack" );
template< class T, int elements >Then, a declaration such as// note non-type parameter
Stack< double, 100 >would instantiate (atmostRecentSalesFigures;
T stackHolder[ elements ];In the exercises, you will be asked to use a non-type parameter to create a template for the Array class developed in Chapter 8, "Operator Overloading." This// array to hold stack contents
template< class T > class Xa friendship declaration of the form
friend void f1();makes function f1 a friend of every template class instantiated from the preceding class template.
template< class T > class Xa friendship declaration of the form
friend void f2( X< T > & );for a particular type T such as float makes function f2( X< float > & ) a friend of X< float > only.
template< class T > class Xa friendship declaration of the form
friend void A::f4();
template< class T > class Xa friendship declaration of the form
friend void C< T >::f5( X< T > & );for a particular type T such as float makes member function
C< float >::f5( X< float > & )a friend function of only template class X< float >.
template< class T > class Xa second class Y can be declared with
friend class Y;making every member function of class Y a friend of every template class produced from the class template for X.
template< class T > class Xa second class Z can be declared with
friend class Z< T >;then when a template class is instantiated with a particular type for T such as float, all members of class Z< float > become friends of template class X< float >.